﻿using pbvs.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using pbvs.Models;

namespace pbvs.Controllers
{
    public class FileController : Controller
    {
        //
        // GET: /File/

        public ActionResult Upload()
        {
            return PartialView(new pbvs.Models.File());
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase uploadedFile, FormCollection fc)
        {
            if (uploadedFile != null)
            {
                string fileExt = uploadedFile.FileName.Substring(uploadedFile.FileName.LastIndexOf("."));
                string fileName = Guid.NewGuid().ToString() + fileExt;
                uploadedFile.SaveAs(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/uploads/" + fileName));
                using (PbvsContext db = new PbvsContext())
                {
                    pbvs.Models.File file = new Models.File();
                    UpdateModel(file);
                    file.FileName = fileName;
                    file.FileType = "img";
                    db.Files.Add(file);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Upload");
        }

        public void Image(int width, int height, string image)
        {
            new WebImage(image)
                .Resize(width, height, true, false)
                .Crop(1, 1)
                .Write();
        }
    }
}
